package com.ims.core.saleconfig.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ims.common.annotation.Log;
import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.enums.BusinessType;
import com.ims.core.saleconfig.domain.TSalProduct;
import com.ims.core.saleconfig.service.ITSalProductService;
import com.ims.common.utils.poi.ExcelUtil;
import com.ims.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author IMS
 * @date 2024-05-20
 */
@RestController
@RequestMapping("/saleconfig/product")
public class TSalProductController extends BaseController
{
    @Autowired
    private ITSalProductService tSalProductService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSalProduct tSalProduct)
    {
        startPage();
        List<TSalProduct> list = tSalProductService.selectTSalProductList(tSalProduct);
        return getDataTable(list);
    }


    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:list')")
    @GetMapping("/listByMaterialCode/{materialCode}")
    public List<TSalProduct> listByMaterialCode(@PathVariable("materialCode") String materialCode)
    {
        startPage();
        List<TSalProduct> list = tSalProductService.selectTSalProductListByMaterialCode(materialCode.replaceAll("€","/"));
        return list;
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSalProduct tSalProduct)
    {
        List<TSalProduct> list = tSalProductService.selectTSalProductList(tSalProduct);
        ExcelUtil<TSalProduct> util = new ExcelUtil<TSalProduct>(TSalProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tSalProductService.selectTSalProductById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSalProduct tSalProduct)
    {
        tSalProduct.setCreateBy(getUsername());
        return toAjax(tSalProductService.insertTSalProduct(tSalProduct));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSalProduct tSalProduct)
    {
        tSalProduct.setUpdateBy(getUsername());
        return toAjax(tSalProductService.updateTSalProduct(tSalProduct));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('saleconfig:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSalProductService.deleteTSalProductByIds(ids));
    }
}
